package com.wuzao.streamTest;

import lombok.Data;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author wuzuhao
 * @since 2023-08-22
 */
public class StreamTest {
    class Person {
        private String name;
        private LocalDateTime creationDate;

        public Person(String name, LocalDateTime creationDate) {
            this.name = name;
            this.creationDate = creationDate;
        }

        public String getName() {
            return name;
        }

        public LocalDateTime getCreationDate() {
            return creationDate;
        }

        @Override
        public String toString() {
            return "Person{name='" + name + "', creationDate=" + creationDate + "}";
        }
    }

    List<Person> people = Arrays.asList(
            new Person("Alice", LocalDateTime.of(2024, 5, 15, 10, 30)),
            new Person("Bob", LocalDateTime.of(2023, 7, 25, 8, 45)),
            new Person("Charlie", LocalDateTime.of(2024, 3, 10, 14, 0))
    );
    @Test
    public void testSort(){
        // 使用 Stream 对 List 进行排序
        List<Person> sortedPeople = people.stream()
                .sorted((p1, p2) -> p1.getCreationDate().compareTo(p2.getCreationDate()))
                .collect(Collectors.toList());

        // 打印排序后的 List
        sortedPeople.forEach(System.out::println);

    }

    @Test
    public void testSort2(){
        // 使用 Stream 对 List 进行排序
        List<Person> sortedPeople = people.stream()
                .sorted(Comparator.comparing(Person::getCreationDate))
                .collect(Collectors.toList());

        // 打印排序后的 List
        sortedPeople.forEach(System.out::println);
    }

}
